def reverse(x):
    """
    :type x: int
    :rtype: int
    """
    res = 0
    max_limit = 2 ** 31
    min_limit = 2 ** 31 * -1
    sign = 1 if x >= 0 else -1
    x = abs(x)
    while x:
        temp = x % 10
        res = res * 10 + temp
        x //= 10
        if (res > max_limit):
            return 0
        if (res < min_limit):
            return 0
    return res * sign


x = -123
print(reverse(x))  # -321
